#使用python创建树
from collections import defaultdict
from scan import getToken
from json import dumps
def tree():
    return defaultdict(tree) #dict的默认值为tree，一个函数

# typedef enum {StmtK,ExpK} NodeKind;
# typedef enum {IfK,RepeatK,AssignK,ReadK,WriteK} StmtKind;
# typedef enum {OpK,ConstK,IdK} ExpKind;


parsertree = tree() #
parsertree['a']['adf']=3

tee=tree()
tee['a']['bb']=45
parsertree['a']['tee']=tee
#如果某个键不存在时，它就会利用tree()来为该键创建一个初始值
#即一个dict，所以{a:dict}
#tree是提供给defaultdict的default_factory
#如果提供该参数，参数的值就传给defaultdict构造器作为第一个参数。


print(dumps(parsertree))
print(parsertree['b'])
class parse:
    token=''

    def parse(self):
        self.parsertree=tree()
        self.token=getToken()
        self.stmt_sequence()

    def stmt_sequence(self) -> tree():
        t = statement()
        p = t

        alist = ['endfile','end','else','until']
        while self.token not in alist:
            match('semi')
            q = statement()
            if q!=None:
                if t==None:t =p =q
                else:


    def statement(self) -> tree():


    def match(self, expected):
        if self.token == expected:
            token = getToken()
        else:
            print('unexpected token -> ')

    def if_stmt(self):

        self.match('if')

    def term(self):


    def factor(self):
        if self.token == 'num':
            t=tree()
            t['attr']['val'] = int(self.tokenstring)
            self.match('num')
        elif self.token == 'id':
            t=tree()
            t['attr']['name'] = self.tokenstring
            self.match('id')
        elif self.token == 'lparen':
            self.match('lparen')
            t = self.exp()
            self.match('rparen')
        else:
            print("a wrong happen")

